package com.erp.erp_servers.Inquiry.imp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.erp_dao.Inquiry.IInquiryDAO;
import com.erp.erp_dao.Inquiry.IInquiryFileDAO;
import com.erp.erp_dao.Inquiry.IInquiryProductDAO;
import com.erp.erp_dao.Inquiry.IInquiryRecordDAO;
import com.erp.erp_entitys.Inquiry.*;
import com.erp.erp_entitys.Inquiry.req.inquiryStatisticsReq;
import com.erp.erp_entitys.Inquiry.res.inquiryStatisticsRes;
import com.erp.erp_entitys.money.rsp.CollectionStatistics2Rsp;
import com.erp.erp_servers.Inquiry.IInquiryService;
import com.erp.utils.DateUtils;
import com.erp.utils.StringUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ImpInquiryService implements IInquiryService {
    @Autowired
    private IInquiryDAO inquiryDAO;
    @Autowired
    private IInquiryProductDAO inquiryProductDAO;
    @Autowired
    private IInquiryFileDAO inquiryFileDAO;
    @Autowired
    private IInquiryRecordDAO inquiryRecordDAO;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        InquiryEntity inquiry = inquiryDAO.selectInfoById(id);
        if(inquiry==null){return 0;}
        if(!CollectionUtils.isEmpty(inquiry.getProductList())){
            List<Integer> idList = new ArrayList<Integer>();
            for (InquiryProductEntity dbProduct : inquiry.getProductList()){
                idList.add(dbProduct.getId());
            }
            if (idList.size()>0){
                inquiryProductDAO.deleteByIdList(idList);
            }
        }
        if(!CollectionUtils.isEmpty(inquiry.getFileList())){
            List<Integer> idList = new ArrayList<Integer>();
            for (InquiryFileEntity dbFile : inquiry.getFileList()){
                idList.add(dbFile.getId());
            }
            if (idList.size()>0){
                inquiryFileDAO.deleteByIdList(idList);
            }
        }
        return inquiryDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(InquiryEntity record) {
        int num = 0;
        num = inquiryDAO.insert(record);
        if(num>0){
            List<InquiryProductEntity> productList = record.getProductList();
            if (!CollectionUtils.isEmpty(productList)){
                for (InquiryProductEntity pro : productList){
                    pro.setInquiryId(record.getId());
                    inquiryProductDAO.insert(pro);
                }
            }
            List<InquiryFileEntity> fileList = record.getFileList();
            if (!CollectionUtils.isEmpty(fileList)){
                for (InquiryFileEntity file : fileList){
                    file.setInquiryId(record.getId());
                    inquiryFileDAO.insert(file);
                }
            }
        }
        return num;
    }

    @Override
    public int insertSelective(InquiryEntity record) {
        return inquiryDAO.insertSelective(record);
    }

    @Override
    public InquiryEntity selectByPrimaryKey(Integer id) {
        return inquiryDAO.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(InquiryEntity record) {
        return inquiryDAO.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(InquiryEntity record) {
        return inquiryDAO.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<InquiryEntity> selectInquiryList(InquiryVo iVo) {
        List<InquiryEntity> inquiryList = inquiryDAO.selectInquiryList(iVo);
        //获取分页信息
        PageInfo pageInfo = new PageInfo(inquiryList);
        pageInfo.setList(inquiryList);
        return pageInfo;
    }

    @Override
    public List<InquiryEntity> selectByIdList(List<Integer> list) {
        return inquiryDAO.selectByIdList(list);
    }

    @Override
    public int updateBoxStatusBykey(InquiryVo iVo){
        return inquiryDAO.updateBoxStatusBykey(iVo);
    }

    @Override
    public int insertInfo(InquiryEntity record) {
        if(record==null){return 0;}
        int num = inquiryDAO.insert(record);
        if(num>0){
            List<InquiryProductEntity> productList = record.getProductList();
            if (!CollectionUtils.isEmpty(productList)){
                for(InquiryProductEntity pro : productList){
                    pro.setInquiryId(record.getId());
                    inquiryProductDAO.insert(pro);
                }
            }
            List<InquiryFileEntity> fileList = record.getFileList();
            if (!CollectionUtils.isEmpty(fileList)){
                for(InquiryFileEntity file : fileList){
                    file.setInquiryId(record.getId());
                    file.setFileExecutor(record.getExecutor());
                    String fileName = file.getFileName();
                    if(!StringUtils.isEmpty(fileName)){
                        if (StringUtils.isEmpty(file.getFileType())){
                            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                            file.setFileType(suffix);
                        }
                    }
                    file.setFileUploadTime(new Date(System.currentTimeMillis()));
                    inquiryFileDAO.insert(file);
                }
            }
        }
        return num;
    }

    @Override
    public int updateInquiryById(InquiryEntity record){
        if (record==null || null == record.getId() || "".equals(record.getId())){
            return 0;
        }
        int num = inquiryDAO.updateByPrimaryKey(record);
        if(num>0){
            List<InquiryProductEntity> productList = record.getProductList();
            List<InquiryProductEntity> dbProductList = inquiryProductDAO.selectProductByInId(record.getId());
            if (!CollectionUtils.isEmpty(dbProductList)){
                List<Integer> idList = new ArrayList<Integer>();
                for (InquiryProductEntity dbProduct : dbProductList){
                    idList.add(dbProduct.getId());
                }
                if (idList.size()>0){
                    inquiryProductDAO.deleteByIdList(idList);
                }
            }
            if (!CollectionUtils.isEmpty(productList)){
                for (InquiryProductEntity pro : productList){
                    pro.setInquiryId(record.getId());
                    inquiryProductDAO.insert(pro);
                }
            }
            List<InquiryFileEntity> fileList = record.getFileList();
            List<InquiryFileEntity> dbFileList = inquiryFileDAO.selectFileByInId(record.getId());
            if (!CollectionUtils.isEmpty(dbFileList)){
                List<Integer> idList = new ArrayList<Integer>();
                for (InquiryFileEntity dbFile : dbFileList){
                    idList.add(dbFile.getId());
                }
                if (idList.size()>0){
                    inquiryFileDAO.deleteByIdList(idList);
                }
            }
            if (!CollectionUtils.isEmpty(fileList)){
                for (InquiryFileEntity file : fileList){
                    file.setInquiryId(record.getId());
                    file.setFileExecutor(record.getExecutor());
                    String fileName = file.getFileName();
                    if(!StringUtils.isEmpty(fileName)){
                        if (StringUtils.isEmpty(file.getFileType())){
                            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                            file.setFileType(suffix);
                        }
                    }
                    file.setFileUploadTime(new Date(System.currentTimeMillis()));
                    inquiryFileDAO.insert(file);
                }
            }
        }
        return num;
    }

    @Override
    public int reInquiryById(InquiryEntity inquiry){
        if (inquiry==null || null == inquiry.getId() || "".equals(inquiry.getId())){
            return 0;
        }
        List<InquiryProductEntity> productList = inquiry.getProductList();
        List<InquiryProductEntity> dbProductList = inquiryProductDAO.selectProductByInId(inquiry.getId());
        if (!CollectionUtils.isEmpty(dbProductList)){
            List<Integer> idList = new ArrayList<Integer>();
            for (InquiryProductEntity dbProduct : dbProductList){
                InquiryRecordEntity record = new InquiryRecordEntity();
                BeanUtils.copyProperties(dbProduct,record);
                record.setId(null);
                record.setInquiryId(inquiry.getId());
                record.setProductId(dbProduct.getId());
                record.setFrequency(inquiry.getFrequency());
                if (inquiryRecordDAO.insert(record)>0){
                    idList.add(dbProduct.getId());
                }
            }
            if (idList.size()>0){
                inquiryProductDAO.deleteByIdList(idList);
            }
        }
        if (!CollectionUtils.isEmpty(productList)){
            for (InquiryProductEntity pro : productList){
                pro.setInquiryId(inquiry.getId());
                inquiryProductDAO.insert(pro);
            }
        }
        List<InquiryFileEntity> fileList = inquiry.getFileList();
        List<InquiryFileEntity> dbFileList = inquiryFileDAO.selectFileByInId(inquiry.getId());
        if (!CollectionUtils.isEmpty(dbFileList)){
            List<Integer> idList = new ArrayList<Integer>();
            for (InquiryFileEntity dbFile : dbFileList){
                idList.add(dbFile.getId());
            }
            if (idList.size()>0){
                inquiryFileDAO.deleteByIdList(idList);
            }
        }
        if (!CollectionUtils.isEmpty(fileList)){
            for (InquiryFileEntity file : fileList){
                file.setInquiryId(inquiry.getId());
                inquiryFileDAO.insert(file);
            }
        }
        int num = inquiryDAO.updateByPrimaryKey(inquiry);
        return num;
    }

    @Override
    public boolean isExist(String oddNumber) {
        List<InquiryEntity> existList = inquiryDAO.isExist(oddNumber);
        return CollectionUtils.isEmpty(existList)?false:true;
    }

    @Override
    public int getLatestOddNumber() {
        return inquiryDAO.getLatestOddNumber();
    }

    /**
     * 获取String类型不为空数据
     * @param value 目标数据
     * @return 不为空数据
     */
    private String getNotNullOfStringInfo(String value){
        if(org.apache.commons.lang3.StringUtils.isEmpty(value)){
            return "";
        }else {
            return value;
        }
    }

    /**
     * 获取BigDecimal类型不为空的数据
     * @param value 目标数据
     * @return 不为空数据
     */
    private BigDecimal getNotNullOfBigDecimalInfo(BigDecimal value){
        if(StringUtil.isNotEmpty(value)){
            return value;
        }else {
            return new BigDecimal("0");
        }
    }

    @Override
    public PageInfo<inquiryStatisticsRes> inquiryStatisticsList(inquiryStatisticsReq req) {
        List<inquiryStatisticsRes> list=inquiryDAO.inquiryStatisticsList(req);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }

    @Override
    public JSONObject inquiryStatisticsExport(inquiryStatisticsReq req) {
        //调用接口查询
        List<inquiryStatisticsRes> list = inquiryDAO.inquiryStatisticsList(req);
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleInfoOfOneStatistics();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(list)){
            obj.put("data", data);
            return obj;
        }
        List<inquiryStatisticsRes> list2 =list.stream().map(p ->{
            p.setInquiryTimeStr(DateUtils.format(p.getInquiryTime(),DateUtils.DATE_PATTERN));
            return p;
        }).collect(Collectors.toList());
        excelCollectionSecondInfoExport(data, list2);
        obj.put("data", data);
        return obj;
    }
    /**
     * 获取excel的title信息
     * @return excel的title信息
     */
    private JSONArray getTitleInfoOfOneStatistics(){
        JSONArray titleList = new JSONArray();
        titleList.add("重要程度");
        titleList.add("询盘日期");
        titleList.add("询价单号");
        titleList.add("销售名英文名");
        titleList.add("销售名中文名");
        titleList.add("客户姓名");
        titleList.add("邮箱");
        titleList.add("国家");
        titleList.add("平台询价单号");
//        titleList.add("图片");
        titleList.add("产品名称");
        titleList.add("中文描述");
        titleList.add("英文描述");
        titleList.add("客户类型");
        titleList.add("业务类型");
        titleList.add("客户来源");
        titleList.add("公司网站");

        return titleList;
    }
    private void excelCollectionSecondInfoExport(JSONArray data, List<inquiryStatisticsRes> list) {
        for (inquiryStatisticsRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(getNotNullOfStringInfo(vo.getImportance()));
            infoList.add(getNotNullOfStringInfo(vo.getInquiryTimeStr()));
            infoList.add(getNotNullOfStringInfo(vo.getOddNumber()));
            infoList.add(getNotNullOfStringInfo(vo.getSalesmanCName()));
            infoList.add(getNotNullOfStringInfo(vo.getSalesmanEName()));
            infoList.add(getNotNullOfStringInfo(vo.getCustomerName()));
            infoList.add(getNotNullOfStringInfo(vo.getEmail()));
            infoList.add(getNotNullOfStringInfo(vo.getCountry()));
            infoList.add(getNotNullOfStringInfo(vo.getPlatformInquiryNo()));
//            infoList.add(getNotNullOfStringInfo(vo.getImg()));
            infoList.add(getNotNullOfStringInfo(vo.getProductName()));
            infoList.add(getNotNullOfStringInfo(vo.getChineseDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getEnglishDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getCustomerType()));
            infoList.add(getNotNullOfStringInfo(vo.getBusinessType()));
            infoList.add(getNotNullOfStringInfo(vo.getSourceOfCustomers()));
            infoList.add(getNotNullOfStringInfo(vo.getWebsite()));

            data.add(infoList);
        }

    }

}
